package com.doyoung.appicker.view;

import java.util.ArrayList;

import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.Loader;
import android.support.v4.app.LoaderManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.doyoung.appicker.R;
import com.doyoung.appicker.controller.SongController;
import com.doyoung.appicker.util.NetworkConnector;
import com.doyoung.appicker.util.XMLParser;

public class MainDummyFragment extends Fragment implements LoaderManager.LoaderCallbacks<String>{
	
	private static MainDummyFragment INSTANCE;
	
	public static MainDummyFragment getInstance(){
		if (INSTANCE == null){
			INSTANCE = new MainDummyFragment();
		}
		return INSTANCE;
	}
	
	private String TAG = "MainDummyFragment";
	
	static final String URL = "http://api.androidhive.info/music/music.xml";
    // XML node keys
    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_TITLE = "title";
    static final String KEY_ARTIST = "artist";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";
    
    ListView list;
    SongController adapter;
	
	public static final String ARG_SECTION_NUMBER = "section_number";
	
	private Document doc;
	private XMLParser parser = new XMLParser();
	
	ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		Log.d(TAG, "MainDummyFragment onActivityCreated() Called");
		
		// When NetworkConnector is AsyncTask
//		NetworkConnector nc = new NetworkConnector(getActivity().getApplicationContext(), URL);
//		nc.execute();
		
		// When NetworkConnector is AsyncTaskLoader
		getLoaderManager().initLoader(0, null, this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		Log.d(TAG, "MainDummyFragment onCreateView() Called");
		
//		TextView textView = new TextView(getActivity());
//		textView.setGravity(Gravity.CENTER);
//		textView.setText("test");
		
//		return textView;
		
//		View view = inflater.inflate(R.layout.fragment_main_dummy, container, false);
//		TextView textView = (TextView) view.findViewById(R.id.textView1);
//		textView.setText("Doyoung");
//		
//		return view;
		
		View view = inflater.inflate(R.layout.fragment_main_dummy, container, false);
		list = (ListView)view.findViewById(R.id.dummy_listView);
		
		// Getting adapter by passing xml data ArrayList
		adapter = new SongController(this.getActivity(), songsList);
		list.setAdapter(adapter);

		return view;
	}

	@Override
	public Loader<String> onCreateLoader(int id, Bundle args) {
		
		return new NetworkConnector(getActivity().getApplicationContext(), URL);
	}

	@Override
	public void onLoadFinished(Loader<String> arg0, String arg1) {
		Log.d(TAG, "onLoadFinished -> arg1 :: " + arg1);
		
		doc = parser.getDomElement(arg1);
		NodeList nl = doc.getElementsByTagName(KEY_SONG);
		
		// looping through all song nodes
		for (int i=0; i<nl.getLength(); i++){
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			
			map.put(KEY_ID, parser.getValue(e, KEY_ID));
			map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
			map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
			map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
			map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
			
			songsList.add(map);
		}
		
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onLoaderReset(Loader<String> arg0) {
		// TODO Auto-generated method stub
		
	}
}
